package com.bdk.middle.cms.web;


import com.bdk.middle.cms.api.domain.BannerDO;
import com.bdk.middle.cms.api.query.BannerQueryDO;
import com.bdk.middle.cms.service.BannerService;
import com.bdk.middle.common.domain.PageDO;
import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2021-03-18
 */
@RestController
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    public @ResponseBody
    ObjectResponse add(@RequestBody BannerDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = bannerService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse edit(@RequestBody BannerDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = bannerService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse delete(@RequestBody BannerDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = bannerService.remove(domain.getId(), null);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse detail(@RequestBody BannerDO domain) {

        ObjectResponse<BannerDO> response = new ObjectResponse();

        ResultDO result = bannerService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((BannerDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody BannerQueryDO domain) {

        ObjectResponse<PageDO<BannerDO>> response = new ObjectResponse();

        ResultDO result = bannerService.find(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((PageDO<BannerDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
}

